package com.ysd.dao.impl;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import com.ysd.entity.NewsClassify;
import com.ysd.util.DBManager;

public class NewsDaoImpl {

	public List<NewsClassify> getNews(){
		//创建集合
		List<NewsClassify> newList = new ArrayList<NewsClassify>();
		//创建连接
		Connection conn = DBManager.getConnection();
		Statement st = null;
		ResultSet rs = null;
		try {//正常
			//创建Statement对象
			st = conn.createStatement();
			//创建并执行sql语句
			rs = st.executeQuery("select * from newsclassify");
			while(rs.next()) {
				//遍历
				int newsClassifyId = rs.getInt("newsCategoryId");
				String newsClassifyName = rs.getString("newsClassifyName");
				NewsClassify news = new NewsClassify(newsClassifyId, newsClassifyName);
				newList.add(news);
			}
			return newList;
		} catch (SQLException e) {//错误时运行
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		} finally {
			DBManager.closeAll(conn, st, rs);
		}
	}
	
	/**
	 * 分页
	 * @param page
	 * @param size
	 * @return 集合
	 */
	public List<NewsClassify> getNewsCateByPage(int page,int size){
		//创建集合
		List<NewsClassify> newList = new ArrayList<NewsClassify>();
		//创建连接
		Connection conn = DBManager.getConnection();
		Statement st = null;
		ResultSet rs = null;
		try {//正常
			//创建Statement对象
			st = conn.createStatement();
			//创建并执行sql语句
			String sql = "select * from newsclassify limit "+(page-1)*size+","+size;//limit后不支持数据计算
			System.out.println(sql);
			rs = st.executeQuery(sql);
			while(rs.next()) {
				//遍历
				int newsClassifyId = rs.getInt("newsCategoryId");
				String newsClassifyName = rs.getString("newsClassifyName");
				NewsClassify news = new NewsClassify(newsClassifyId, newsClassifyName);
				newList.add(news);
			}
			return newList;
		} catch (SQLException e) {//错误时运行
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		} finally {
			DBManager.closeAll(conn, st, rs);
		}
	}
	
	/**
	 * 根据咨询返回的Id删除产品
	 * @param id newsClassifyId
	 * @return 数据库受影响行数
	 */
	public int getdelNewsCatbyId(int newsId) {
		return DBManager.executeUpdate("delete from newsclassify where newsCategoryId="+newsId);
	}
	
	/**
	 * 根据咨询分类名称添加数据
	 * @param n
	 * @return 受影响行数
	 */
	public int addNewsCategory(NewsClassify n) {
		return DBManager.executeUpdate("insert into newsclassify values(null,'"+n.getNewsClassifyName()+"')");
	}
	/**
	 * 根据名称查询咨询分类
	 * @param name
	 * @return
	 */
	public List<NewsClassify> getNewsWhereByName(String name){
		List<NewsClassify> ncList = new ArrayList<NewsClassify>();
		Connection conn = DBManager.getConnection();
		Statement st = null;
		ResultSet rs = null;
		
		try {
			st = conn.createStatement();
			rs = st.executeQuery("select * from newsclassify where newsClassifyName='"+name+"'");
			while(rs.next()) {
				int newsCategoryId = rs.getInt("newsCategoryId");
				String newsClassifyName = rs.getString("newsClassifyName");
				NewsClassify news = new NewsClassify(newsCategoryId, newsClassifyName);
				ncList.add(news);
			}
			return ncList;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		} finally {
			DBManager.closeAll(conn, st, rs);
		}
	}
	/**
	 * 根据id查询分类
	 * @param nCateId
	 * @return int
	 */
	public NewsClassify getNewsCateById(int cId) {
		Connection conn = DBManager.getConnection();
		Statement st = null;
		ResultSet rs = null;
		NewsClassify nc = null;
		
		try {
			st = conn.createStatement();
			rs = st.executeQuery("select * from newsclassify where newsCategoryId="+cId);
			while(rs.next()) {
				int ncId = rs.getInt(1);
				String ncName = rs.getString(2);
				nc = new NewsClassify(ncId, ncName);
			}
			return nc;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		} finally {
			DBManager.closeAll(conn, st, rs);
		}
	}
	
	/**
	 * 根据数据库的字段修改
	 * @param ns
	 * @return 新闻分类对象
	 */
	public int updateNewsCateById(NewsClassify ns) {
		String sql="update newsclassify set newsClassifyName='"+ns.getNewsClassifyName()+"' where newsCategoryId="+ns.getNewsCategoryId();
		System.out.println(sql);
		return DBManager.executeUpdate(sql);
	}
	
	/**
	 * 批量删除
	 * @param ids
	 * @return 多个id为String
	 */
	public int delNewCateByIds(String ids) {
		String sql = "delete from newsclassify where newsCategoryId in("+ids+")";
		System.out.println(sql);
		return DBManager.executeUpdate(sql);
	}
	
}
